package yu.code.provider;

import yu.code.provider.service.UserServiceImpl;
import yu.code.service.UserService;
import yu.code.yurpc.RpcApplication;
import yu.code.yurpc.config.RegistryConfig;
import yu.code.yurpc.config.RpcConfig;
import yu.code.yurpc.model.ServiceMetaInfo;
import yu.code.yurpc.registry.LocalRegistry;
import yu.code.yurpc.registry.Registry;
import yu.code.yurpc.registry.RegistryFactory;
import yu.code.yurpc.server.VertxHttpServer;

//简单的服务提供者
public class EasyProviderExample {
    public static void main(String[] args) {
        //RPC 框架初始化
        RpcApplication.init();
        //注册服务
        LocalRegistry.register(UserService.class.getName(), UserServiceImpl.class);
        //注册服务到注册中心
        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(UserService.class.getName());
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
        try {
            registry.register(serviceMetaInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //启动 TCP 服务
        VertxHttpServer vertxHttpServer = new VertxHttpServer();
        vertxHttpServer.start(8080);
        /*//启动Http服务
        VertxHttpServer vertxHttpServer = new VertxHttpServer();
        vertxHttpServer.start(9090);*/
    }
}
